/**
 * @file
 * @auth jinguangguo
 * @date 2017/1/18
 */

import Vue from 'vue';
import VueRouter from 'vue-router';
import VueI18n from 'vue-i18n';
import messages from '../module/account/lang';

import './base/init';
import LangPopover from '../component/LangPopover.vue';
import commonService from '../../src/common/commonService';

import Basic from '../module/account/basic/main';
import Certification from '../module/account/certification/main';
import Asset from '../module/account/asset/main';
import SysMessage from '../module/account/message/main';
import MyOrder from '../module/account/myOrder/main';
import lockPlan from '../module/account/lockPlan/main';
import lockReward from '../module/account/lockReward/main';
import Deposit from '../module/account/asset/deposit/main';
import Withdraw from '../module/account/asset/withdraw/main';
import Wallet from '../module/account/wallet/main';
import C2cSeting from '../module/account/c2c/main';

import moment from 'moment';

//我的账户
import MyAssets from '../component/MyAssets.vue';
import Loading from '../../grsc-base/component/Loading.vue';

Vue.use(VueI18n);

Vue.prototype.$$moment = moment;

let i18n =  new VueI18n({
    locale: window.app.LANG,
    messages, // set locale messages
});

const router = new VueRouter({
    'routes': [
        {
            path: '/',
            name: 'index',
            component: Asset,
            beforeEnter: (to, from, next) => {
                beforeEnterAccount(to, from, next);
            }
        },
        {
            path: '/basic',
            name: 'assets',
            component: Basic,
            beforeEnter(to, from, next) {
                fixBug(to, from, next)
            }
        },
        {
            path: '/certification',
            name: 'certification',
            component: Certification,
            beforeEnter(to, from, next) {
                fixBug(to, from, next)
            }
        },
        {
            path: '/digitalasset',
            name: 'asset',
            component: Asset,
            beforeEnter: (to, from, next) => {
                beforeEnterAccount(to, from, next);
            }
        },
        // 消息
        {
            path: '/message',
            name: 'messageDefault',
            redirect: '/message/total',
            beforeEnter(to, from, next) {
                fixBug(to, from, next)
            }
        },
        {
            path: '/message/:type',
            name: 'message',
            component: SysMessage,
            beforeEnter(to, from, next) {
                fixBug(to, from, next)
            }
        },
        {
            path: '/my-order',
            name: 'my-order',
            component: MyOrder,
            beforeEnter(to, from, next) {
                fixBug(to, from, next)
            }
        },
        {
            path: '/lock-plan',
            name: 'lock-plan',
            component: lockPlan,
            beforeEnter(to, from, next) {
                fixBug(to, from, next)
            }
        },
        {
            path: '/lock-reward',
            name: 'lock-reward',
            component: lockReward,
            beforeEnter(to, from, next) {
                fixBug(to, from, next)
            }
        },
        {
            path: '/deposit/:coinType',
            name: 'deposit',
            component: Deposit,
            beforeEnter: (to, from, next) => {
                beforeEnterAccount(to, from, next);
            }
        },
        {
            path:'/withdraw/:coinType',
            name:'withdraw',
            component:Withdraw,
            beforeEnter: (to, from, next) => {
                beforeEnterAccount(to, from, next);
            }
        },
        {
            path: '/wallet/:coinType',
            name: 'wallet',
            component: Wallet,
            beforeEnter(to, from, next) {
                beforeEnterAccount(to, from, next);
            }
        },
        {
            path: '/c2c',
            name: 'c2c',
            component: C2cSeting,
            beforeEnter(to, from, next) {
                fixBug(to, from, next)
            }
        }
    ]
});

// 莫名其妙的bug，慎动
function fixBug(to, from, next) {
    if (from.name === 'withdraw') {
        window.location.reload()
    } else {
        next()
    }
}

function beforeEnterAccount(to, from, next) {

    // fixBug
    if (from.name === 'withdraw') {
        window.location.reload()
    }
    // fixbug end


    if (!Vue.prototype.$$assetList) {
        commonService.fetchAssets((assetMap, assetList) => {
            Vue.prototype.$$assetMap = assetMap;
            Vue.prototype.$$assetList = assetList;
            console.log('refresh')
            next();
        });
    } else {
        next()
    }

}


const app = new Vue({
    router,
    i18n,
    components: {
        MyAssets,
        Loading,
        LangPopover
    },
    data() {
        return {
            clientHeight :1000
        }
    },
    created() {
        this.clientHeight = document.body.clientHeight - ( 48 + 32 );
        this.queryGoogleStatus();
    },
    watch: {
        '$route.path'(newVal, oldVal) {
            $('body').scrollTop(0);
        }
    },
    methods:{
        queryGoogleStatus(){
            commonService.googleCodeQuery().done((rep) => {
                if (rep.code === window.CODE.SUCCESS) {
                    window.localStorage.setItem('GOOGLE_BOUND', rep.data.isBound);
                    window.localStorage.setItem('GOOGLE_STATUS', rep.data.status);
                } else {
                    window.error(rep.msg);
                }
            }).fail((rep) => {
                window.error(rep)
            })
        },
    }
}).$mount('#app');
